﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

namespace CTEA_INDIA
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BAL.conn.connectionString = ConfigurationManager.ConnectionStrings["CTEA"].ConnectionString;
            idSpanMsg.Style.Add("display", "none");

            if (!IsPostBack)
            {
                txtUserName.Attributes.Add("onFocus", "clearOnFocus();");
                txtPassword.Attributes.Add("onFocus", "clearOnFocus();");
                clear();

            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            //DateTime LastLogin = BAL.User.GetLastLogin(Convert.ToInt32(Session["UserId"]), UserTypeID);
            //Session.Add("Lastlogin", LastLogin);
            //BAL.User.UpdateLastLogin(Convert.ToInt32(Session["userid"]), UserTypeID);
            // Session.Add("UserTypeId", UserTypeID);

            string username = txtUserName.Text;
            string password = txtPassword.Text;
            int login = Convert.ToInt32(rbtLogin.SelectedValue);
            Session.Add("login", login);
            string strPassword = BAL.Encryption.Encript(password, "&%#@?,:*");

            int UserID = BAL.Customers.verifyuser(username, strPassword, login);


            if (UserID == -1 || UserID == -2)
            {
                idSpanMsg.Style.Add("display", "block");

                idSpanMsg.InnerHtml = "Sorry, Incorrect Login";
            }
            else if (UserID == 0)
            {
                idSpanMsg.Style.Add("display", "block");

                idSpanMsg.InnerHtml = "Your Email ID is not verified yet. <br> Click on Confirmation link sent to your Email ID to verify.";
            }
            else
            {
                //Session.Add("UserID", strVal);

                Session.Add("UserID", UserID);
                Session.Add("Login", login);


                // Session.Add("CustomerEmail", strEmail);
                int UserTypeID = BAL.Customers.GetUserType(Convert.ToInt32(Session["UserID"]), login);
                Session.Add("UserTypeId", UserTypeID);


                if (Convert.ToInt32(Session["UserTypeID"]) == 2)
                {
                    int TechTypeID = BAL.Customers.GetTechType(Convert.ToInt32(Session["UserID"]), login);
                    Session.Add("TechID", TechTypeID);

                    string LocationID = BAL.Customers.GetLocationID(Convert.ToInt32(Session["UserID"]), login);
                    Session.Add("Location", LocationID);


                }



                //    DateTime LastLogin = BAL.Customers.GetLastLogin(Convert.ToInt32(Session["UserId"]), UserTypeID);
                // Session.Add("Lastlogin", LastLogin);
                BAL.Customers.UpdateLastLogin(Convert.ToInt32(Session["UserID"]), Convert.ToInt32(Session["Login"]));
                Session.Add("UserTypeId", UserTypeID);


                if (login == 0)
                {
                    string LocationID = BAL.Customers.GetLocationID(Convert.ToInt32(Session["UserID"]), login);
                    Session.Add("Location", LocationID);
                    if (UserTypeID == 1)//admin
                    {


                        Response.Redirect("Pending_Candidate.aspx");
                    }
                    if (UserTypeID == 2)//Technical
                    {


                        Response.Redirect("MyProfileAT.aspx");

                    }
                    if (UserTypeID == 3)//Company/user
                    {
                        Response.Redirect("UserProfile.aspx");

                    }
                }
                else if (login == 1)//candidate
                {

                    Response.Redirect("Candidate_Profile.aspx");
                }
            }
        }


        public void clear()
        {
            txtUserName.Text = "";
            txtPassword.Text = "";
        }

    }
}
